﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class ManageAdvertise : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }      

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            QuangCao q = new QuangCao();           
            foreach (GridViewRow r in GridView1.Rows)
            {
                CheckBox cbx = (CheckBox)r.FindControl("cbCheck");
                if (cbx != null && cbx.Checked)
                {
                    string result = q.DeleteAdvertise(Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value));
                }
            }
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Visible = false;
            btnAdd.Visible = false;
            cbCheckAll.Visible = false;

            Session["ID"] = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            Panel2.Visible = true;
            txtTen.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[2].Text);
            if (GridView1.SelectedRow.Cells[3].Text == "Banner Advertise")
            {
                ddlVitri.SelectedIndex = 1;
            }
            else if (GridView1.SelectedRow.Cells[3].Text == "Banner Right")
            {
                ddlVitri.SelectedIndex = 2;
            }
            else if (GridView1.SelectedRow.Cells[3].Text == "Banner Left") {
                ddlVitri.SelectedIndex = 4;
            }
            else
            {
                ddlVitri.SelectedIndex = 3;
            }
            bool flag = ((CheckBox)GridView1.SelectedRow.Cells[4].Controls[0]).Checked;
            if (flag)
            {
                CheckBox1.Checked = true;
            }
            else
            {
                CheckBox1.Checked = false;
            }
            CKEditor1.Text = ((Label)GridView1.SelectedRow.FindControl("Label2")).Text;
        }

        protected void cbCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbCheck");
                if (cbCheckAll.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }

        protected void btnSua_Click(object sender, EventArgs e)
        {
            QuangCao db = new QuangCao();
            if (ddlVitri.SelectedIndex == 0)
            {
                Label1.Text = "Mời bạn chọn vị trí hiển thị của quảng cáo!";
                Label1.Visible = true;
            }
            else
            {
                int id = Convert.ToInt32(Session["ID"].ToString());
                string ten = txtTen.Text;
                string vitri = ddlVitri.SelectedValue;
                bool hienthi;
                if (CheckBox1.Checked)
                {
                    hienthi = true;
                }
                else
                {
                    hienthi = false;
                }
                string noidung = CKEditor1.Text;
                db.UpdateAdvertise(id, ten, noidung, vitri, hienthi);
                GridView1.DataBind();
                Panel2.Visible = false;
                btnDelete.Visible = true;
                btnAdd.Visible = true;
                cbCheckAll.Visible = true;
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Panel2.Visible = false;
            btnDelete.Visible = true;
            btnAdd.Visible = true;
            cbCheckAll.Visible = true;
        }      

    }
}